﻿using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace RSuite.Common
{
    public static class ZipUtil
    {
        public static void ZipFolder(string folder, string output)
        {
            if (!folder.EndsWith("\\"))
            {
                folder += "\\";
            }

            using (var zipStream = new ZipOutputStream(File.Create(output)))
            {
                foreach (var file in Directory.GetFiles(folder, "*", SearchOption.AllDirectories))
                {
                    using (var fs = File.OpenRead(file))
                    {
                        var buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, buffer.Length);

                        var entry = new ZipEntry(file.Substring(folder.Length));
                        zipStream.PutNextEntry(entry);
                        zipStream.Write(buffer, 0, buffer.Length);
                    }
                }
                zipStream.SetLevel(5);
                zipStream.Finish();
            }
        }
    }
}